package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Classinfo;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.queryobject.ClassQueryObject;
import cn.wolfcode.resultpage.JsonResult;
import cn.wolfcode.service.IClassService;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/class")
public class ClassController {
    @Autowired
    private IClassService classService;
    @Autowired
    private IEmployeeService employeeService;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ClassQueryObject qo){
        List<Employee> employees = employeeService.listAll();
        PageInfo<Classinfo> pageInfo = classService.query(qo);
        model.addAttribute("employees",employees);
        model.addAttribute("PageInfo",pageInfo);
        return "class/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate (Classinfo classinfo) {

        try {
            if (classinfo.getId() != null) {
                classService.update(classinfo);
            } else {
                classService.insert(classinfo);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败");
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null){
            classService.delete(id);
            return new JsonResult();
        }
        return new JsonResult(false,"删除失败");
    }

}
